package cn.etgps.etgps.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.view.Display;
import android.view.WindowManager;

import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.aigestudio.datepicker.BuildConfig;


/**
 * Created by xiaoyu on 2016/3/9.
 * 工具类
 */
public class CommonUtils {
    // email匹配
    private static final String EMAIL_MATCH_REGX =
            "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))"
                    + "([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    //11位手机号
    private static final String MOBILE_MATCH_REGX = "[1]\\d{10}";// "[1]"代表第1位为数字1，"\\d{10}"代表后面是可以是0～9的数字，有10位。
    //车牌号
    private static final String LICENSENUM_MATCH_REGX = "[\\u4e00-\\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}";
    //18位身份证
    private static final String IDENTITY_MATCH_REGX =
            "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$";
    /**
     * 防止Button快速连点
     **/

    private static long lastClickTime;

    /**
     * 判断是否是合法身份证
     **/
    public static boolean isID(String identity) {
        Pattern p = Pattern.compile(IDENTITY_MATCH_REGX);
        Matcher m = p.matcher(identity);
        return m.matches();
    }

    /**
     * 判断是否是邮箱
     **/
    public static boolean isEmail(String email) {
        Pattern p = Pattern.compile(EMAIL_MATCH_REGX);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 验证手机号码 1开头就行
     */
    public static boolean isMobileNO(String mobiles) {
        return mobiles.matches(MOBILE_MATCH_REGX);
    }

    /**
     * 判断String是否为空
     **/
    public static boolean isEmpty(String string) {
        return string == null || string.length() == 0 || string.equals("null");
    }

    /**
     * 获取设备唯一编码
     **/
    public static String getImieStatus(Context _context) {
        TelephonyManager tm = (TelephonyManager) _context.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = tm.getDeviceId();
        if (BuildConfig.DEBUG) {
            return "12345678911111";
        }
        return deviceId;
    }

    /**
     * guid
     **/
    public static String getDeviceGUID(Context _context) {
        TelephonyManager tm = (TelephonyManager) _context.getSystemService(Context.TELEPHONY_SERVICE);
        final String tmDevice, tmSerial, androidId;
        tmDevice = "" + tm.getDeviceId();
        tmSerial = "" + tm.getSimSerialNumber();
        androidId = "" + Settings.Secure.getString(_context.getContentResolver(), Settings.Secure.ANDROID_ID);
        UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
        return deviceUuid.toString();
    }

    /**
     * 判断是否是车牌号
     **/
    public static boolean isLicenseNum(String licenseNum) {
        Pattern p = Pattern.compile(LICENSENUM_MATCH_REGX);
        Matcher m = p.matcher(licenseNum);
        return m.matches();
    }

    /**
     * 是否有网络
     **/
    public static boolean hasNetWork(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null) {
            return networkInfo.isAvailable();
        }
        return false;
    }

    /***
     * 得到屏幕分辨率高
     */
    @SuppressWarnings("deprecation")
    public static int getDisplayHeight(Context context) {
        Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        return display.getHeight();

    }

    /***
     * 得到屏幕分辨率宽
     */
    @SuppressWarnings("deprecation")
    public static int getDisplayWidth(Context context) {
        Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        return display.getWidth();

    }

    /**
     * dp转px
     **/
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * px转dp
     **/
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f) - 15;
    }

    /**
     * 去掉.0
     **/
    public static String getFloatWithoutZero(float price) {
        String priceStr = String.valueOf(price);
        if (priceStr.endsWith(".0")) {
            priceStr = priceStr.substring(0, priceStr.length() - 2);
        }
        return priceStr;
    }

    public static boolean isFastClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < 500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }
}
